




//
//  FreshBookCell.m
//  ZQLearn
//
//  Created by 朵 on 2019/4/28.
//  Copyright © 2019年 macfeel. All rights reserved.
//

#import "FreshBookCell.h"
#import "VipCollectionViewCell.h"
#import "HomeModel.h"
@interface FreshBookCell()<UICollectionViewDelegate,UICollectionViewDataSource>

///横向滚动的collectionView
@property(nonatomic,strong)UICollectionView *hCollectionView;

///
@property(nonatomic,strong)NSMutableArray *array;

@end

//static NSString *freshCellID = @"fresh";
#define vipCellIdentifier @"vip"

@implementation FreshBookCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self makeUI];
//        self.array = [NSMutableArray array];
        self.backgroundColor = kWhiteColor;
    }
    return self;
}



-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    //    [self makeUI];
//    [self.array removeAllObjects];
//    [self.array addObjectsFromArray:dataArray];
    _hCollectionView.delegate = self;
    _hCollectionView.dataSource = self;
    
}



- (void)makeUI{
    
//    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, ScreenWidth-40, 90)];
//    image.image = WDImgName(@"vip");
//    [self addSubview:image];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置itemSize
    layout.itemSize = CGSizeMake(ScreenWidth/1.5,107.f);
    ///横向的
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //这三个属性至关重要
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(8, 0, 8, 0);
    
    //2.初始化collectionView
    _hCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,107.f) collectionViewLayout:layout];
    _hCollectionView.bounces = YES;
    _hCollectionView.delegate = self;
    _hCollectionView.dataSource = self;
    _hCollectionView.showsHorizontalScrollIndicator = false;
    _hCollectionView.backgroundColor = kWhiteColor;
    
    ///cell 注册时候一定要分清楚是手写的，还是用xib 画的，如果是xib画的，就要用nib 的那个注册方法，否则出不来
    //    [_hCollectionView registerClass:[VipCollectionViewCell class] forCellWithReuseIdentifier:vipCellID];
    ///xib方式
    [_hCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([VipCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:vipCellIdentifier];
    
    [self addSubview:_hCollectionView];
}


#pragma mark - datasource  delegate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    //    return self.iconArray.count;
    return self.dataArray.count;
//    return self.array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ///如果一个文件中写了多个.h 那么只需要引进第一个的头文件，后面的就可以自动出来
    MidModel *model = self.dataArray[indexPath.row];
//    MidModel *model = self.array[indexPath.row];
    VipCollectionViewCell *cell = (VipCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:vipCellIdentifier forIndexPath:indexPath];

    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:model.firstPhoto] placeholderImage:nil];
    cell.nameLab.text = model.name;
    
    cell.msgLab.text = model.content?:@"";
    YYLog(@"%@",model.name);
    
    [cell.readBtn addTarget:self action:@selector(readBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //    if (_iconArray.count > indexPath.row) {
    //        WDFourIconModel *model = _iconArray[indexPath.row];
    //        [cell configData:model];
    //    }
    
    return cell;
}

///按钮事件
- (void)readBtnAction:(UIButton *)btn{
    
    NSLog(@"hehh***");
    UIView *contentView = (UIView *)[btn superview];
    VipCollectionViewCell *cell = (VipCollectionViewCell *)[contentView superview];
    NSIndexPath *indexPath = [self.hCollectionView indexPathForCell:cell];
    MidModel *model = self.dataArray[indexPath.row];
    ///当是第0行时 indexPath.row 可能是nil
    model.picUrl = model.firstPhoto;
    model.index = indexPath.row;
    ///
    YYLog(@"%@***jjj",model);
    if (_indexBlock) {
        _indexBlock(model);
    }
    
}

@end
